package lib.servlet;

import lib.Dao.CourseDAO;
import lib.Dao.Dbutil;
import lib.Dao.UserDao;
import lib.Model.Course;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ttop5 on 16-4-22.
 */
@WebServlet(urlPatterns = "/aufecourse", name = "aufecourse")
public class CourseServletForYuliang extends HttpServlet{
    Dbutil dbutil = new Dbutil();
    CourseDAO courseDAO=new CourseDAO();
    UserDao userDao=new UserDao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        List<Course> courseList=new ArrayList<>();
        String openId= (String) req.getSession().getAttribute("openid");


        try {
            String se=userDao.getUserSe(openId);
            courseList= courseDAO.getCourse(se);
            for(int i=0;i<courseList.size();i++){
                courseList.get(i).setRongliang(courseList.get(i).getRongliang()-courseList.get(i).getCnum());
            }
            req.getSession().setAttribute("se", se);
            //schoolList= schoolDAO.getSchoollike(content);

             //JSONArray data = JSONArray.fromObject(schoolList);
            //System.out.println(data);

            req.setAttribute("list", courseList);
            //System.out.println(schoolList);

            req.getRequestDispatcher("/page/course/aufelist.jsp").forward(req, resp);
            //resp.sendRedirect("/page/dept/qrcode.jsp");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
